package com.ddone.code.executor.factory;

import com.ddone.code.executor.pojo.enums.ExecutorType;
import com.ddone.code.executor.service.ExecutorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ddone
 * @date 2024/8/2-21:08
 */
@Component
public class ExecutorServiceFactory {
    private final Map<ExecutorType, ExecutorService> executorServiceMap = new HashMap<>();

    @Autowired
    public ExecutorServiceFactory(Map<String, ExecutorService> executorServices) {
        executorServices.forEach((name, service) -> {
            switch (name) {
                case "mysqlExecutorServiceImpl":
                    executorServiceMap.put(ExecutorType.Mysql, service);
                    break;
                case "hiveExecutorServiceImpl":
                    executorServiceMap.put(ExecutorType.Hive, service);
                    break;
                case "sparkSqlExecutorServiceImpl":
                    executorServiceMap.put(ExecutorType.SparkSql, service);
                    break;
                default:
                    throw new IllegalArgumentException("Unknown service name: " + name);
            }
        });
    }

    public ExecutorService getExecutorService(ExecutorType type) {
        return executorServiceMap.get(type);
    }
}
